package com.ch.light.web.service.impl;

import com.ch.light.mybatis.ParamFilter;
import com.ch.light.web.dao.DictDao;
import com.ch.light.web.entity.pojo.JSTree;
import com.ch.light.web.mapper.DictMapper;
import com.ch.light.web.model.Dict;
import com.ch.light.web.service.DictService;
import com.ch.light.web.util.UserContextUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Override
    public int save(Dict record) {
        checkNotNull(record, "字典信息不能为空");
        checkArgument(!Strings.isNullOrEmpty(record.getCode()), "字典Code不能为空");
        checkArgument(!Strings.isNullOrEmpty(record.getName()), "字典名称不能为空");
        checkArgument(!Strings.isNullOrEmpty(record.getIsCategory()), "字典类型不能为空");
        if (record.getParentId() == null) {
            record.setParentId("#");
        }
        Dict param = new Dict();
        param.setCode(record.getCode());
        List<Dict> records = this.find(param);
        checkArgument(records == null || records.isEmpty(), "字典Code不能重复");
        record.setStatus("1");
        record.setCreateBy(UserContextUtil.getUserId());
        record.setCreateAt(new Date());
        return dictMapper.insertSelective(record);
    }

    @Override
    public List<Dict> findPageList(ParamFilter queryFilter) {
        int pageNum = queryFilter.getPage().getPageNo();
        int pageSize = queryFilter.getPage().getPageSize();
        PageHelper.startPage(pageNum, pageSize, true, false, false);
        Example example = new Example(Dict.class);
        return dictMapper.selectByExample(example);
    }

    @Override
    public int update(Dict dict) {
        return dictMapper.updateByPrimaryKeySelective(dict);
    }

    @Override
    public List<JSTree> getTree() {
        return dictMapper.getTree();
    }

    @Override
    public List<Dict> findById(Long id) {

        Dict record = dictMapper.selectByPrimaryKey(id);
        List<Dict> records = Lists.newArrayList(record);

        Dict param = new Dict();
        param.setParentId(id.toString());
        List<Dict> subList = this.find(param);
        if (subList != null && !subList.isEmpty()) {
            records.addAll(subList);
        }
        return records;
    }

    @Override
    public List<Dict> find(Dict record) {
        if (record == null) record = new Dict();
        return dictMapper.select(record);
    }

    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            dictMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Dict getById(Long id) {
        return dictMapper.selectByPrimaryKey(id);
    }


}
